package com.example.forum_system.service.impl;

import com.example.forum_system.dao.ArticleReplyMapper;
import com.example.forum_system.dao.UserMapper;
import com.example.forum_system.model.ArticleReply;
import com.example.forum_system.model.VO.ArticleReplyVO;
import com.example.forum_system.service.IArticleReplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: 0.forum_system
 * @BelongsPackage: com.example.forum_system.service.impl
 * @Author : 游在海里的鱼
 * @CreateTime : 2023-11-13  17:15
 * @Version : 1.0
 * @Description : TODO
 */
@Service
@Slf4j
public class ArticleReplyService implements IArticleReplyService {
    @Autowired
    ArticleReplyMapper articleReplyMapper;
    @Autowired
    UserMapper userMapper;

    /*------------------------------------普--通--方--法------------------------------------------------------------------
     * @Desc  : 私有方法,初始化[文章回复]
     * @Param : [articleReply]
     * @Return: void
    **/
    private void initialize(ArticleReply articleReply){
        if(articleReply.getLikeCount() == null){
            articleReply.setLikeCount(0);
        }
        if(articleReply.getState() == null){
            articleReply.setState(Byte.valueOf("0"));
        }
        if(articleReply.getDeleteState() == null){
            articleReply.setDeleteState(Byte.valueOf("0"));
        }
        if(articleReply.getCreateTime() == null){
            articleReply.setCreateTime(new Date());
        }
        if(articleReply.getUpdateTime() == null){
            articleReply.setUpdateTime(new Date());
        }
    }
    
    /*------------------------------------普--通--方--法------------------------------------------------------------------
     * @Desc  : TODO
     * @Param : [articleReply]
     * @Return: int
    **/
    @Override
    public int insertReply(ArticleReply articleReply) {
        initialize(articleReply);
        return articleReplyMapper.insertReply(articleReply);
    }

    @Override
    public List<ArticleReplyVO> selectAllReply(long articleId) {
         List<ArticleReplyVO> replyVOList = articleReplyMapper.selectRepliesByArticle(articleId);
         addUser(replyVOList);
         return replyVOList;
    }

    private void addUser(List<ArticleReplyVO> articleReplyVOList){
        for(ArticleReplyVO reply:articleReplyVOList){
            reply.setUser(userMapper.selectOneById(reply.getPostUserId()));
        }
    }

}
